﻿//-----------------------------------------------------------------------
// <copyright file="SetEntityMatrixAndSizeAction.cs" company="Nathan Miller">
// Copyright (c) Nathan Miller.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
//-----------------------------------------------------------------------
namespace TileTool.Scene.Action
{
    using System;
    using System.Collections.Generic;
    using TileTool.Action;
    using TileTool.Common.Math;
    using TileTool.Log;
    using TileTool.Scene.Entity;

    /// <summary>
    /// Action to set the matrix and size of entities.
    /// </summary>
    public class SetEntityMatrixAndSizeAction : SceneAction
    {
        #region Constants - Private

        /// <summary>
        /// Log message group.
        /// </summary>
        private const string LogGroup = "SetEntityMatrixAndSizeAction";

        #endregion

        #region Fields - Private

        /// <summary>
        /// Dictionary which tracks the entities we will set the matrix of.
        /// </summary>
        private IDictionary<IEntity, EntityData> entityDictionary = new Dictionary<IEntity, EntityData>();

        #endregion

        #region Constructor

        /// <summary>
        /// Constructor for the action.
        /// </summary>
        /// <param name="manager">Scene this action operates on.</param>
        public SetEntityMatrixAndSizeAction(ISceneManager manager)
            : base(manager)
        {
            this.Valid = false;
        }

        #endregion

        #region Methods - Public

        /// <summary>
        /// Add an entity to the action so its matrix will be set during a do / undo.
        /// </summary>
        /// <param name="entity">Entity to set the matrix on.</param>
        /// <param name="startMat">Start matrix.</param>
        /// <param name="endMat">End matrix.</param>
        /// <param name="startSize">Start size.</param>
        /// <param name="endSize">End size.</param>
        public void AddEntity(IEntity entity, Matrix2D startMat, Matrix2D endMat, Size2D startSize, Size2D endSize)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity", "Entity must be non-null.");
            }

            if (entity.Scene != this.Scene)
            {
                throw new InvalidOperationException("Entity scene does not match scene for action.");
            }

            this.entityDictionary.Remove(entity);
            this.entityDictionary[entity] = new EntityData(startMat, endMat, startSize, endSize);
            this.Valid = true;
        }

        /// <summary>
        /// Do the action.
        /// </summary>
        public override void Do()
        {
            if (!this.Valid)
            {
                return;
            }

            LogSystem.LogInfo(LogGroup, "Do() - Setting matrix for " + this.entityDictionary.Count + " entities.");

            IEntityCollection collection = new EntityCollection(this.entityDictionary.Keys);

            foreach (KeyValuePair<IEntity, EntityData> item in this.entityDictionary)
            {
                IEntity entity = item.Key;
                EntityData data = item.Value;

                entity.Matrix = data.EndMatrix;
                entity.Size = data.EndSize;
            }
        }

        /// <summary>
        /// Undo the action.
        /// </summary>
        public override void Undo()
        {
            if (!this.Valid)
            {
                return;
            }

            LogSystem.LogInfo(LogGroup, "Undo() - Setting matrix for " + this.entityDictionary.Count + " entities.");

            IEntityCollection collection = new EntityCollection(this.entityDictionary.Keys);

            foreach (KeyValuePair<IEntity, EntityData> item in this.entityDictionary)
            {
                IEntity entity = item.Key;
                EntityData data = item.Value;

                entity.Matrix = data.StartMatrix;
                entity.Size = data.StartSize;
            }
        }

        #endregion

        #region Class - Private

        /// <summary>
        /// Class which wraps data for an entity.
        /// </summary>
        private class EntityData
        {
            /// <summary>
            /// Constructor for the data class.
            /// </summary>
            /// <param name="startMat">Start matrix.</param>
            /// <param name="endMat">End matrix.</param>
            /// <param name="startSize">Start size.</param>
            /// <param name="endSize">End size.</param>
            public EntityData(Matrix2D startMat, Matrix2D endMat, Size2D startSize, Size2D endSize)
            {
                this.StartMatrix = startMat;
                this.EndMatrix = endMat;
                this.StartSize = startSize;
                this.EndSize = endSize;
            }

            /// <summary>
            /// Gets the start matrix.
            /// </summary>
            public Matrix2D StartMatrix
            {
                get;
                private set;
            }

            /// <summary>
            /// Gets the end matrix.
            /// </summary>
            public Matrix2D EndMatrix
            {
                get;
                private set;
            }

            /// <summary>
            /// Gets the start size.
            /// </summary>
            public Size2D StartSize
            {
                get;
                private set;
            }

            /// <summary>
            /// Gets the end size.
            /// </summary>
            public Size2D EndSize
            {
                get;
                private set;
            }
        }

        #endregion
    }
}
